<template>
	<view>
		<button type="primary" @click="pay">立即付款</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			};
		},
		methods:{
			
			// 生成预付单
			payOrder(openid){
				uni.request({
					url:'http://api.100qhl.com:5024/v1/order/createOrder',
					method:"post",
					data:{
						openId:openid,
						name:'法拉利',
						money:0.01,
						code:'asd1asd2121214'+parseInt(Math.random()*100000) ,
						payType:1,
						clientIp:'111.255.35.6'  //IPv4  55.55.35.61 --支付成功的 后端服务器的回调地址
					},
					success(res) {
						console.log('----');
						console.log(res);
						// appId: "wx4f27fd51b9a7bfda"
						// nonceStr: "34ZswxMrQu78x7s1"
						// packageValue: "prepay_id=wx071200176623765902d48da661fb5d0000"
						// paySign: "2697572A827BB4F2369D0D944C4FAE5F"
						// signType: "MD5"
						// timeStamp: "1680838913"
						//调用微信支付
						let {appId,nonceStr,packageValue,paySign,signType,timeStamp} = res.data.data;
						uni.requestPayment({
							provider:'wxpay',
							timeStamp,
							nonceStr,
							package:packageValue,
							signType,
							paySign,
							success(res) {
								//支付成功
								console.log('微信告诉小程序支付成功:---后端不知道的');
								console.log('调用后端的接口,告诉后端,这个订单号已经支付成功,后端修改 订单状态---支付流程结束');
							},
							fail(err) {
								//支付失败
								console.log(err);
							}
						})
						
					}
				})
			},
			
			//点击支付-拿openid 	
			pay(){
				let _this = this;
				uni.login({
					success(res) {
						console.log(res);
						let {code} = res;
						//根据code 请求 微信的后端 拿到openid
						uni.request({
							url:'https://api.weixin.qq.com/sns/jscode2session',
							method:'get',
							data:{
								appid:'wx4f27fd51b9a7bfda',
								secret:'1a9bfee5a8155f0020c5857cd9038674',
								js_code:code,
								grant_type:'authorization_code'
							},
							success(res) {
									console.log(res);
									// openid: "oIaa94qqS877TL_oE7yhpq-ONZ9Y"
									// session_key: "bjtrKO2ntLSL4DyinT7irg=="
									// unionid: "odvsU6NQ5UVMoy0VOZp4XOv0fA1k"
									_this.payOrder(res.data.openid)
									
									
							}
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">

</style>
